import unittest
import requests

from api.login_api import RegLoginApi
from api.recharge_api import RechargeApi
from common.utils import common_assert, html_util, read_json
from parameterized import parameterized


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 实例化登录模块封装的类
        self.login_api = RegLoginApi(self.session)
        # 实例化充值模块封装的类
        self.recharge_api = RechargeApi(self.session)
        # 调用登陆接口，完成用户登陆
        body = {"keywords": "13389898980", "password": "123456"}
        self.login_api.user_login(body)

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 获取充值验证码测试用例
    @parameterized.expand(read_json("/recharge_data", "recharge_verify_code"))
    def test01_get_recharge_verify_code(self, r, exp_status_code):
        # 获取充值图片验证码
        resp = self.recharge_api.get_recharge_verify_code(r)
        # 断言
        self.assertEqual(exp_status_code, resp.status_code)

    # 充值测试用例
    @parameterized.expand(read_json("/recharge_data", "recharge"))
    def test02_recharge(self, money, verify_code, exp_status_code, exp_status, description):
        # 登录，类前置已登陆
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code(100)
        # 充值
        resp = self.recharge_api.recharge(money, verify_code)
        # 断言
        common_assert(self, resp, exp_status_code, exp_status, description)

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录，类前置已登陆
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code(100)
        # 充值
        resp_recharge = self.recharge_api.recharge("10000", "8888")
        # 获取第三方开户接口的url
        url = html_util(resp_recharge)[0]
        # 获取第三方开户接口的请求体数据
        form_dict = html_util(resp_recharge)[1]
        # 获取第三方接口返回的响应结果
        resp_3 = self.recharge_api.third_recharge(url, form_dict)
        # 断言
        self.assertIn("OK", resp_3.text)
